<?php
header('Content-Type:text/html;charset=UTF-8');

$url = 'http';
if (isset($_SERVER["HTTPS"]) && strtolower($_SERVER["HTTPS"]) == 'on') {
	$url .= "s";
} 
$url .= trim('://' . $_SERVER["SERVER_NAME"], '/');
if ($_SERVER["SERVER_PORT"] != "80") {
	$url .= ':' . $_SERVER["SERVER_PORT"];
} 
$url .= '/index.php?d=api%2Fmember&c=notify&m=';
$json = '{"sign":"59216a8b82d015d04915d222a971b9bf","result_details":"2015091500001000760061198215^0.01^SUCCESS#2015091500001000760061193878^0.01^SUCCESS","notify_time":"2015-09-16 15:18:52","sign_type":"MD5","notify_type":"batch_refund_notify","notify_id":"8e37ccc82c255f830a2043505cffccel0k","batch_no":"201509165780","success_num":"2"}';
$post = json_decode($json, true);
//$post = $_POST;
$header = array();
$global = isset($GLOBALS['HTTP_RAW_POST_DATA'])?$GLOBALS['HTTP_RAW_POST_DATA']:false;
$time = date('Y-m-d H:i:s');

if(empty($post) && $global) {
	$post = $global;
	$header[] = 'Content-type:text/xml; charset=utf-8';
	$m = 'wxpay';
} elseif(isset($post['trade_no']) && !empty($post['trade_no'])) {
	$m = 'alipay';
	$time = date('Y-m-d H:i:s');
	file_put_contents('./data/logs/alipay/'.date('Ymd').'.log', json_encode($post)." 【{$time}】\n\n", FILE_APPEND);
} elseif(isset($post['batch_no']) && !empty($post['batch_no'])) {
	$m = 'alipay_refund';
	$time = date('Y-m-d H:i:s');
	file_put_contents('./data/logs/alipay/refund_'.date('Ymd').'.log', json_encode($post)." 【{$time}】\n\n", FILE_APPEND);
} else {	
	exit();
}
$url .= $m;

if(!empty($post)) {
	$ch = curl_init();
	//设置超时
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	curl_setopt($ch, CURLOPT_URL, $url);
	if(!empty($header)) {
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
	}
	//设置header
	curl_setopt($ch, CURLOPT_HEADER, FALSE);
	//要求结果为字符串且输出到屏幕上
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

	//post提交方式
	curl_setopt($ch, CURLOPT_POST, TRUE);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	//运行curl
	$data = curl_exec($ch);
	curl_close($ch);
	//返回结果
	echo $data;
}
die();